package com.example.managemyself.data.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.example.managemyself.data.model.Task;

import java.util.List;

@Dao
public interface TaskDao {
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long insertTask(Task task);
    
    @Update
    void updateTask(Task task);
    
    @Delete
    void deleteTask(Task task);
    
    @Query("DELETE FROM tasks WHERE id = :id")
    void deleteTaskById(int id);
    
    @Query("SELECT * FROM tasks WHERE id = :id")
    LiveData<Task> getTaskById(int id);
    
    @Query("SELECT * FROM tasks ORDER BY id DESC")
    LiveData<List<Task>> getAllTasks();
    
    @Query("SELECT * FROM tasks WHERE status = :status ORDER BY id DESC")
    LiveData<List<Task>> getTasksByStatus(String status);
    
    @Query("SELECT * FROM tasks WHERE type = :type ORDER BY id DESC")
    LiveData<List<Task>> getTasksByType(String type);
    
    @Query("SELECT * FROM tasks WHERE importance = :importance ORDER BY id DESC")
    LiveData<List<Task>> getTasksByImportance(int importance);
    
    @Query("SELECT * FROM tasks WHERE urgency = :urgency ORDER BY id DESC")
    LiveData<List<Task>> getTasksByUrgency(String urgency);
    
    @Query("DELETE FROM tasks WHERE id IN (:taskIds)")
    void deleteTasksByIds(List<Integer> taskIds);
    
    // 兼容性方法
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long insert(Task task);
    
    @Update
    void update(Task task);
    
    @Delete
    void delete(Task task);
    
    // 同步查询方法供AIDataManager使用
    @Query("SELECT * FROM tasks ORDER BY id DESC")
    List<Task> getAllTasksSync();
    
    @Query("SELECT * FROM tasks WHERE status = :status ORDER BY id DESC")
    List<Task> getTasksByStatusSync(String status);
    
    // AI助手专用的同步查询方法
    @Query("SELECT * FROM tasks WHERE isCompleted = 0 ORDER BY createdDate DESC")
    List<Task> getPendingTasksSync();
    
    @Query("SELECT * FROM tasks WHERE createdDate >= :startDate ORDER BY createdDate DESC")
    List<Task> getTasksAfterDateSync(java.util.Date startDate);
    
    @Query("SELECT * FROM tasks WHERE title LIKE :keyword OR description LIKE :keyword ORDER BY createdDate DESC LIMIT 10")
    List<Task> searchTasksSync(String keyword);
    
    @Query("SELECT * FROM tasks WHERE status = '已完成' AND DATE(completedDate) = DATE(:date) ORDER BY completedDate DESC")
    List<Task> getCompletedTasksByDateSync(java.util.Date date);
    
    // 异步获取完成任务的方法
    @Query("SELECT * FROM tasks WHERE status = '已完成' AND DATE(completedDate) = :dateStr ORDER BY completedDate DESC")
    LiveData<List<Task>> getCompletedTasksByDate(String dateStr);

}
